﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KorbeRTR
{
    public partial class FormWatch : Form
    {
        private KorbeRTR.Models.OmronData plc => FormMainWindow.OmronPLC;

        public FormWatch()
        {
            InitializeComponent();
        }

        private void FormWatch_Load(object sender, EventArgs e)
        {
            UpdateGrid(GridWatch);

            timer.Enabled = true; ;
        }

        private void UpdateGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;
            grid.DisplayRowNumber = true;

            grid.Rows = 1 + plc.device.Variables.Values.Count;
            grid.Cols = 4;

            grid.Column(1).Width = 200;

            int row = 0;
            grid.Cell(row, 0).Text = "序号";
            grid.Cell(row, 1).Text = "名称";
            grid.Cell(row, 2).Text = "地址";
            grid.Cell(row, 3).Text = "值";
            row++;

            foreach (var iter in plc.device.Variables.Values)
            {
                grid.Cell(row, 1).Text = iter.Comment;
                grid.Cell(row, 2).Text = iter.HslAddr;               
                grid.Cell(row, 3).Text = iter.ValueString();

                row++;
            }

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            UpdateGrid(GridWatch);
        }
    }
}
